package com.spark.sniffer.plugin.plugins.define.trace;

import com.spark.sniffer.plugin.interceptor.InstanceMethodsInterceptPoint;
import com.spark.sniffer.plugin.interceptor.StaticMethodsInterceptPoint;
import com.spark.sniffer.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.spark.sniffer.plugin.match.ClassMatch;
import com.spark.sniffer.plugin.match.MethodAnnotationMatch;
import com.spark.sniffer.plugin.plugins.interceptor.ExceptionHandlerInterceptor;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

/**
 * ExceptionHandler注解插件
 * 
 * @author Cobe
 *
 */
public class ExceptionHandlerPlugin extends ClassInstanceMethodsEnhancePluginDefine {

    @Override
    protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {new InstanceMethodsInterceptPoint() {
            @Override
            public ElementMatcher<MethodDescription> getMethodsMatcher() {
                return ElementMatchers
                    .isAnnotatedWith(ElementMatchers.named("org.springframework.web.bind.annotation.ExceptionHandler"));
            }

            @Override
            public Class<?> getMethodsInterceptor() {
                return ExceptionHandlerInterceptor.class;
            }

            @Override
            public boolean isOverrideArgs() {
                return false;
            }
        }};
    }

    @Override
    protected StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() {
        return null;
    }

    @Override
    protected ClassMatch enhanceClass() {
        return MethodAnnotationMatch
            .byMethodAnnotationMatch(new String[] {"org.springframework.web.bind.annotation.ExceptionHandler"});
    }

}
